/*
 *  levelUpGame - OscDataSender.cpp
 *  Copyright 2011 W. Reckman. All rights reserved.
 */
#include "ofUtils.h"
#include "Settings.h"
#include "OscDataSender.h"


const string OscDataSender::OSC_ADDR_GAME_BALANCE = "/levelUp/game/balance";
const string OscDataSender::OSC_ADDR_GAME_STATE_OLD = "/levelUp/game/state/old";
const string OscDataSender::OSC_ADDR_GAME_STATE_NEW = "/levelUp/game/state/new";

const string OscDataSender::OSC_ADDR_GAME_PLAYER_DATE = "/levelUp/game/player/date"; //2*int (month, day)
const string OscDataSender::OSC_ADDR_GAME_PLAYER_TIME = "/levelUp/game/player/time"; //3*int (hours, minutes, seconds)
const string OscDataSender::OSC_ADDR_GAME_PLAYER_TARGETS = "/levelUp/game/player/targets"; //2*int (culture, industry)
const string OscDataSender::OSC_ADDR_GAME_PLAYER_ID = "/levelUp/game/player/id"; //int (id)
const string OscDataSender::OSC_ADDR_GAME_PLAYER_IMAGES = "/levelUp/game/player/images"; //2*string (topImg, bottomImg)
const string OscDataSender::OSC_ADDR_GAME_PLAYER_COLOR = "/levelUp/game/player/color"; //3*int (r, g, b)
const string OscDataSender::OSC_ADDR_GAME_PLAYER_PICKUPS = "/levelUp/game/player/pickups"; //2*int (goodPickups, badPickups)

OscDataSender::OscDataSender()
: s( Settings::getInstance() )
{}

void OscDataSender::setup() {
	scoreboardHost = *s.getValueInGroup( "scoreboard_hostname", "settings", "127.0.0.1" );
	scoreboardPort = ofToInt( *s.getValueInGroup( "scoreboard_port", "settings", "8766" ) );
	
	oscSender.setup( scoreboardHost, scoreboardPort );
}

void OscDataSender::sendGameBalance( float floatBalance_ ) const {
	ofxOscMessage msg;
	msg.setAddress( OSC_ADDR_GAME_BALANCE );
	msg.addFloatArg( floatBalance_ );
	oscSender.sendMessage( msg );
}

void OscDataSender::sendGameState( GAME_STATE oldState_, GAME_STATE newState_ ) const {
	ofxOscBundle bundle;
	ofxOscMessage msg1, msg2;
	
	msg1.setAddress( OSC_ADDR_GAME_STATE_OLD );
	msg1.addStringArg( getOscGameStateName( oldState_ ) );
	
	msg2.setAddress( OSC_ADDR_GAME_STATE_NEW );
	msg2.addStringArg( getOscGameStateName( newState_ ) );
	
	bundle.addMessage( msg1 ); bundle.addMessage( msg2 );
	oscSender.sendBundle( bundle );
}

void OscDataSender::sendPlayerData( const GameModel::PlayerInfo& data_ ) const {
	ofxOscBundle bundle;
	ofxOscMessage dateMsg, timeMsg, tgtMsg, idMsg, imgMsg, colorMsg, pickupMsg;
	
	dateMsg.setAddress( OSC_ADDR_GAME_PLAYER_DATE );
	dateMsg.addIntArg( data_.month ); dateMsg.addIntArg( data_.day );
	
	timeMsg.setAddress( OSC_ADDR_GAME_PLAYER_TIME );
	timeMsg.addIntArg( data_.hours ); timeMsg.addIntArg( data_.minutes ); timeMsg.addIntArg( data_.seconds );
	
	tgtMsg.setAddress( OSC_ADDR_GAME_PLAYER_TARGETS );
	tgtMsg.addIntArg( data_.cultureTgt ); tgtMsg.addIntArg( data_.industryTgt );
	
	idMsg.setAddress( OSC_ADDR_GAME_PLAYER_ID );
	idMsg.addIntArg( data_.id );
	
	imgMsg.setAddress( OSC_ADDR_GAME_PLAYER_IMAGES );
	imgMsg.addStringArg( *data_.topImagePath ); imgMsg.addStringArg( *data_.bottomImagePath );
	
	colorMsg.setAddress( OSC_ADDR_GAME_PLAYER_COLOR );
	colorMsg.addIntArg( data_.color.r ); colorMsg.addIntArg( data_.color.g ); colorMsg.addIntArg( data_.color.b );
	
	pickupMsg.setAddress( OSC_ADDR_GAME_PLAYER_PICKUPS );
	pickupMsg.addIntArg( data_.goodPickups ); pickupMsg.addIntArg( data_.badPickups );
	
	bundle.addMessage( dateMsg ); bundle.addMessage( timeMsg ); bundle.addMessage( tgtMsg ); bundle.addMessage( idMsg );
	bundle.addMessage( imgMsg ); bundle.addMessage( colorMsg ); bundle.addMessage( pickupMsg );
	oscSender.sendBundle( bundle );
}
